package com.example.aop.aspect;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

import java.util.Random;

@Aspect
@Component
public class PermissionAspect {
    private Random random = new Random();
    
    // 对add和delete方法进行权限检查
    @Before("execution(* com.example.aop.OrderService.add(..)) || " +
           "execution(* com.example.aop.OrderService.delete(..))")
    public void checkPermission() {
        // 生成0或1的随机数，模拟权限检查
        int permission = random.nextInt(2);
        
        if (permission == 0) {
            System.out.println("权限检查通过，允许执行操作");
        } else {
            System.out.println("没有权限，终止方法运行");
            // 抛出异常终止方法执行
            throw new RuntimeException("权限不足，无法执行操作");
        }
    }
}
